package com.eight.group.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.eight.group.entity.PageResult;
import com.eight.group.entity.QueryPageBean;
import com.eight.group.mapper.PermissionMapper;
import com.eight.group.pojo.Permission;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xqx.eight.group.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = PermissionService.class)
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    //分页查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        //分页助手参数设置
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());

        List<Permission> list = permissionMapper.findPageByCondition(queryPageBean.getQueryString());

        //返回封装后的结果
        return new PageResult(new PageInfo<>(list).getTotal(), list);
    }

    //新增权限
    @Override
    @Transactional
    public void add(Permission permission) {
        permissionMapper.add(permission);
    }

    /*
        查询所有权限
     */
    @Override
    public PageResult findAll() {

        List<Permission> list = permissionMapper.findPageByCondition(null);
        return new PageResult(null, list);
    }
    /*
        编辑权限
     */
    @Override
    @Transactional
    public void edit(Permission permission) {
        permissionMapper.edit(permission);
    }

    /*
      根据permissionId查询权限信息
     */
    @Override
    public Permission findById(Integer id) {

        return permissionMapper.findById(id);
    }

    /*
        根据permissionId删除中间表t_role_permission表数据和t_permission表数据
     */
    @Override
    @Transactional
    public void delete(Integer id) {

        permissionMapper.deleteRoleAndPermissionByRoleId(id);

        permissionMapper.delete(id);

    }


}
